package com.imis.module.system.bus;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.imis.base.constant.CommonConstant;
import com.imis.base.constant.enums.QuartzJobStatusEnum;
import com.imis.base.constant.enums.SysTipEnum;
import com.imis.base.globle.Result;
import com.imis.base.util.ConvertUtils;
import com.imis.module.base.BaseBus;
import com.imis.module.system.model.converter.SysQuartzJobConverter;
import com.imis.module.system.model.po.SysQuartzJob;
import com.imis.module.system.model.ro.PagingQueryQuartzJobDTO;
import com.imis.module.system.model.ro.SysQuartzJobAddRO;
import com.imis.module.system.model.ro.SysQuartzJobUpdateRO;
import com.imis.module.system.model.vo.SysQuartzJobVO;
import com.imis.module.system.service.ISysQuartzJobService;
import lombok.extern.slf4j.Slf4j;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 定时任务 业务处理类
 * </p>
 *
 * @author XinLau
 * @since 2020-06-15
 */
@Slf4j
@Service
public class SysQuartzJobBus extends BaseBus {

    /**
     * 定时任务 服务类
     */
    private ISysQuartzJobService serviceBySysQuartzJobService;

    @Autowired
    public void setISysQuartzJobService(ISysQuartzJobService serviceBySysQuartzJobService) {
        this.serviceBySysQuartzJobService = serviceBySysQuartzJobService;
    }

    /**
     * 定时任务 调度程序
     */
    private Scheduler scheduler;

    @Autowired(required = false)
    public void setScheduler(Scheduler scheduler) {
        this.scheduler = scheduler;
    }

    /**
     * 添加定时任务重复校验
     *
     * @param sysQuartzJobAddRo - 定时任务添加对象
     * @return Result
     * @author XinLau
     * @creed The only constant is change ! ! !
     * @since 2020/3/9 9:47
     */
    private Result quartzJobAddVerification(SysQuartzJobAddRO sysQuartzJobAddRo) {
        // 1.校验定时任务名称重复
        SysQuartzJob sysQuartzJob = serviceBySysQuartzJobService.queryQuartzJobByName(sysQuartzJobAddRo.getJobName());
        if (sysQuartzJob != null) {
            // 1.根据定时任务信息查询，该定时任务名存在
            addLog(CommonConstant.LOG_TYPE_OPERATE, CommonConstant.OPERATE_TYPE_ADD, SysTipEnum.QUARTZ_JOB_ADD_ERR_REPEAT.toString(), sysQuartzJob.getJobName());
            return Result.errorsFormat(SysTipEnum.QUARTZ_JOB_ADD_ERR_REPEAT, sysQuartzJob.getJobName());
        }
        return Result.ok();
    }

    /**
     * 确定具有给定标识符的作业是否已存在于计划程序中
     *
     * @param jobKey - 定时任务 唯一标识
     * @return Boolean -
     * @author XinLau
     * @creed The only constant is change ! ! !
     * @since 2020/6/16 15:59
     */
    private Boolean checkQuartzJobExists(final JobKey jobKey) {
        Boolean checkExists;
        try {
            // 1.确定具有给定标识符的作业是否已存在于计划程序中
            checkExists = scheduler.checkExists(jobKey);
        } catch (SchedulerException e) {
            checkExists = Boolean.FALSE;
        }
        return checkExists;
    }

    /**
     * 删除定时任务校验
     *
     * @param sysQuartzJob - 定时任务对象
     * @return Result
     * @author XinLau
     * @creed The only constant is change ! ! !
     * @since 2020/3/9 9:47
     */
    private Result quartzJobDeleteVerification(final SysQuartzJob sysQuartzJob) {
        // 1.校验定时任务存在
        if (sysQuartzJob == null) {
            addLog(CommonConstant.LOG_TYPE_OPERATE, CommonConstant.OPERATE_TYPE_QUERY, SysTipEnum.QUARTZ_JOB_DELETE_ERR_NON.toString());
            return Result.errorsFormat(SysTipEnum.QUARTZ_JOB_DELETE_ERR_NON);
        }
        JobKey jobKey = new JobKey(sysQuartzJob.getJobName(), sysQuartzJob.getJobGroup());
        log.debug(jobKey.toString());
        if (checkQuartzJobExists(jobKey)) {
            // 2.已存在于计划程序中
            addLog(CommonConstant.LOG_TYPE_OPERATE, CommonConstant.OPERATE_TYPE_QUERY, SysTipEnum.QUARTZ_JOB_DELETE_ERR_ASSIGNED.toString(), sysQuartzJob.getJobName());
            return Result.errorsFormat(SysTipEnum.QUARTZ_JOB_DELETE_ERR_ASSIGNED, sysQuartzJob.getJobName());
        }
        return Result.ok();
    }

    /**
     * 更新定时任务校验
     *
     * @param sysQuartzJobUpdateRo - 定时任务更新对象
     * @return Result
     * @author XinLau
     * @creed The only constant is change ! ! !
     * @since 2020/3/9 9:47
     */
    private Result quartzJobUpdateVerification(final SysQuartzJobUpdateRO sysQuartzJobUpdateRo) {
        // 1.校验定时任务名称重复
        SysQuartzJob sysQuartzJob = serviceBySysQuartzJobService.getById(sysQuartzJobUpdateRo.getId());
        if (sysQuartzJob == null) {
            // 2.根据定时任务标识查询，该定时任务不存在
            addLog(CommonConstant.LOG_TYPE_OPERATE, CommonConstant.OPERATE_TYPE_ADD, SysTipEnum.QUARTZ_JOB_UPDATE_ERR_NON.toString(), sysQuartzJob.getJobName());
            return Result.errorsFormat(SysTipEnum.QUARTZ_JOB_UPDATE_ERR_NON, sysQuartzJob.getJobName());
        }
        JobKey jobKey = new JobKey(sysQuartzJob.getJobName(), sysQuartzJob.getJobGroup());
        log.debug(jobKey.toString());
        if (checkQuartzJobExists(jobKey)) {
            // 2.已存在于计划程序中
            addLog(CommonConstant.LOG_TYPE_OPERATE, CommonConstant.OPERATE_TYPE_QUERY, SysTipEnum.QUARTZ_JOB_DELETE_ERR_ASSIGNED.toString(), sysQuartzJob.getJobName());
            return Result.errorsFormat(SysTipEnum.QUARTZ_JOB_DELETE_ERR_ASSIGNED, sysQuartzJob.getJobName());
        }
        List<SysQuartzJob> sysQuartzJobList = serviceBySysQuartzJobService.list(Wrappers.<SysQuartzJob>lambdaQuery().eq(SysQuartzJob::getJobName, sysQuartzJobUpdateRo.getJobName()).eq(SysQuartzJob::getJobGroup, sysQuartzJobUpdateRo.getJobGroup()));
        if (sysQuartzJobList.size() > 0) {
            for (SysQuartzJob sysQuartzJob1 : sysQuartzJobList) {
                if (!sysQuartzJob1.getId().equals(sysQuartzJob.getId())) {
                    // 3.根据定时任务信息查询，该定时任务存在
                    addLog(CommonConstant.LOG_TYPE_OPERATE, CommonConstant.OPERATE_TYPE_ADD, SysTipEnum.QUARTZ_JOB_UPDATE_ERR_REPEAT.toString(), sysQuartzJob.getJobName());
                    return Result.errorsFormat(SysTipEnum.QUARTZ_JOB_UPDATE_ERR_REPEAT, sysQuartzJob.getJobName());
                }
            }
        }
        return Result.ok();
    }

    /**
     * 构建定时任务校验
     *
     * @param sysQuartzJob - 定时任务对象
     * @return Result
     * @author XinLau
     * @creed The only constant is change ! ! !
     * @since 2020/3/9 9:47
     */
    private Result schedulerQuartzJobVerification(final SysQuartzJob sysQuartzJob) {
        // 1.校验定时任务不存在
        if (sysQuartzJob == null) {
            addLog(CommonConstant.LOG_TYPE_OPERATE, CommonConstant.OPERATE_TYPE_QUERY, SysTipEnum.QUARTZ_JOB_SCHEDULER_ERR_JOB_NON.toString());
            return Result.errorsFormat(SysTipEnum.QUARTZ_JOB_SCHEDULER_ERR_JOB_NON);
        }
        // 2.排除数据库中状态为  正常、暂停、完成、出错、阻塞、具有给定标识符的作业已存在于计划程序中 的定时任务
        JobKey key = new JobKey(sysQuartzJob.getJobName(), sysQuartzJob.getJobGroup());
        if (!sysQuartzJob.getJobState().equals(QuartzJobStatusEnum.NONE.getKey()) || checkQuartzJobExists(key)) {
            addLog(CommonConstant.LOG_TYPE_OPERATE, CommonConstant.OPERATE_TYPE_QUERY, SysTipEnum.QUARTZ_JOB_SCHEDULER_ERR_JOB_REPEAT.toString());
            return Result.errorsFormat(SysTipEnum.QUARTZ_JOB_SCHEDULER_ERR_JOB_REPEAT);
        }
        return Result.ok();
    }

    /**
     * 暂停定时任务校验
     *
     * @param sysQuartzJob - 定时任务对象
     * @return Result
     * @author XinLau
     * @creed The only constant is change ! ! !
     * @since 2020/3/9 9:47
     */
    private Result pauseQuartzJobVerification(final SysQuartzJob sysQuartzJob) {
        // 1.校验定时任务不存在
        if (sysQuartzJob == null) {
            addLog(CommonConstant.LOG_TYPE_OPERATE, CommonConstant.OPERATE_TYPE_QUERY, SysTipEnum.QUARTZ_JOB_PAUSE_ERR_NON.toString());
            return Result.errorsFormat(SysTipEnum.QUARTZ_JOB_PAUSE_ERR_NON);
        }
        // 2.排除数据库中状态为 不存在、具有给定标识符的作业已不存在于计划程序中 的定时任务
        JobKey key = new JobKey(sysQuartzJob.getJobName(), sysQuartzJob.getJobGroup());
        if (sysQuartzJob.getJobState().equals(QuartzJobStatusEnum.NONE.getKey()) || !checkQuartzJobExists(key)) {
            addLog(CommonConstant.LOG_TYPE_OPERATE, CommonConstant.OPERATE_TYPE_QUERY, SysTipEnum.QUARTZ_JOB_PAUSE_ERR_STATE_ERR.toString());
            return Result.errorsFormat(SysTipEnum.QUARTZ_JOB_PAUSE_ERR_STATE_ERR);
        }
        return Result.ok();
    }

    /**
     * 恢复定时任务校验
     *
     * @param sysQuartzJob - 定时任务对象
     * @return Result
     * @author XinLau
     * @creed The only constant is change ! ! !
     * @since 2020/3/9 9:47
     */
    private Result resumeQuartzJobVerification(final SysQuartzJob sysQuartzJob) {
        // 1.校验定时任务不存在
        if (sysQuartzJob == null) {
            addLog(CommonConstant.LOG_TYPE_OPERATE, CommonConstant.OPERATE_TYPE_QUERY, SysTipEnum.QUARTZ_JOB_RESUME_ERR_JOB_NON.toString());
            return Result.errorsFormat(SysTipEnum.QUARTZ_JOB_RESUME_ERR_JOB_NON);
        }
        // 2.数据库中状态为  暂停、出错、具有给定标识符的作业已不存在于计划程序中 的定时任务
        JobKey key = new JobKey(sysQuartzJob.getJobName(), sysQuartzJob.getJobGroup());
        if (sysQuartzJob.getJobState().equals(QuartzJobStatusEnum.PAUSED.getKey())
                || sysQuartzJob.getJobState().equals(QuartzJobStatusEnum.ERROR.getKey())
                || checkQuartzJobExists(key)
        ) {
            return Result.ok();
        }
        addLog(CommonConstant.LOG_TYPE_OPERATE, CommonConstant.OPERATE_TYPE_QUERY, SysTipEnum.QUARTZ_JOB_RESUME_ERR_JOB_STATE_ERR.toString());
        return Result.errorsFormat(SysTipEnum.QUARTZ_JOB_RESUME_ERR_JOB_STATE_ERR);
    }

    /**
     * 移除定时任务校验
     *
     * @param sysQuartzJob - 定时任务对象
     * @return Result
     * @author XinLau
     * @creed The only constant is change ! ! !
     * @since 2020/3/9 9:47
     */
    private Result removeQuartzJobVerification(final SysQuartzJob sysQuartzJob) {
        // 1.校验定时任务不存在
        if (sysQuartzJob == null) {
            addLog(CommonConstant.LOG_TYPE_OPERATE, CommonConstant.OPERATE_TYPE_QUERY, SysTipEnum.QUARTZ_JOB_REMOVE_ERR_JOB_NON.toString());
            return Result.errorsFormat(SysTipEnum.QUARTZ_JOB_REMOVE_ERR_JOB_NON);
        }
        // 2.数据库中状态为  暂停 并且 具有给定标识符的作业已不存在于计划程序中 的定时任务
        JobKey key = new JobKey(sysQuartzJob.getJobName(), sysQuartzJob.getJobGroup());
        if (sysQuartzJob.getJobState().equals(QuartzJobStatusEnum.PAUSED.getKey()) && checkQuartzJobExists(key)) {
            return Result.ok();
        }
        addLog(CommonConstant.LOG_TYPE_OPERATE, CommonConstant.OPERATE_TYPE_QUERY, SysTipEnum.QUARTZ_JOB_REMOVE_ERR_STATE_ERR.toString());
        return Result.errorsFormat(SysTipEnum.QUARTZ_JOB_REMOVE_ERR_STATE_ERR);
    }

    /**
     * 执行定时任务校验
     *
     * @param sysQuartzJob - 定时任务对象
     * @return Result
     * @author XinLau
     * @creed The only constant is change ! ! !
     * @since 2020/3/9 9:47
     */
    private Result triggerQuartzJobVerification(final SysQuartzJob sysQuartzJob) {
        // 1.校验定时任务不存在
        if (sysQuartzJob == null) {
            addLog(CommonConstant.LOG_TYPE_OPERATE, CommonConstant.OPERATE_TYPE_QUERY, SysTipEnum.QUARTZ_JOB_TRIGGER_ERR_NON.toString());
            return Result.errorsFormat(SysTipEnum.QUARTZ_JOB_TRIGGER_ERR_NON);
        }
        // 2.排除数据库中状态为 不存在、具有给定标识符的作业已不存在于计划程序中 的定时任务
        JobKey key = new JobKey(sysQuartzJob.getJobName(), sysQuartzJob.getJobGroup());
        if (sysQuartzJob.getJobState().equals(QuartzJobStatusEnum.NONE.getKey()) || !checkQuartzJobExists(key)) {
            addLog(CommonConstant.LOG_TYPE_OPERATE, CommonConstant.OPERATE_TYPE_QUERY, SysTipEnum.QUARTZ_JOB_RESUME_ERR_JOB_STATE_ERR.toString());
            return Result.errorsFormat(SysTipEnum.QUARTZ_JOB_RESUME_ERR_JOB_STATE_ERR);
        }
        return Result.ok();
    }

    /**
     * 修改定时任务执行状态
     *
     * @param sysQuartzJob - 定时任务
     * @return Result -
     * @author XinLau
     * @creed The only constant is change ! ! !
     * @since 2020/7/23 10:10
     */
    private Result updateQuartzJobState(final SysQuartzJob sysQuartzJob) {
        try {
            // TriggerState = NONE, NORMAL, PAUSED, COMPLETE, ERROR, BLOCKED
            Trigger.TriggerState triggerState = scheduler.getTriggerState(TriggerKey.triggerKey(sysQuartzJob.getJobName(), sysQuartzJob.getJobGroup()));
            // 修改定时任务执行状态
            sysQuartzJob.setJobState(QuartzJobStatusEnum.valueOf(triggerState.name()).getKey());
            serviceBySysQuartzJobService.updateById(sysQuartzJob);
            return Result.ok();
        } catch (SchedulerException e) {
            addLog(CommonConstant.LOG_TYPE_OPERATE, CommonConstant.OPERATE_TYPE_UPDATE, SysTipEnum.SC_ERROR_SCHEDULER_EXCEPTION.toString(), e.getMessage());
            return Result.errorsFormat(SysTipEnum.SC_ERROR_SCHEDULER_EXCEPTION, e.getMessage());
        }
    }

    /**
     * 分页查询定时任务
     *
     * @param pagingQueryQuartzJobDTO - 定时任务分页查询对象
     * @return Result -
     * @author XinLau
     * @creed The only constant is change ! ! !
     * @since 2020/6/16 08:54
     */
    public Result pagingQuerySysQuartzJobListByParameter(final PagingQueryQuartzJobDTO pagingQueryQuartzJobDTO) {
        try {
            Page<SysQuartzJobVO> sysQuartzJobListByParameter = serviceBySysQuartzJobService.pagingQuerySysQuartzJobListByParameter(pagingQueryQuartzJobDTO);
            return Result.ok(sysQuartzJobListByParameter);
        } catch (Exception e) {
            addLog(CommonConstant.LOG_TYPE_OPERATE, CommonConstant.OPERATE_TYPE_QUERY, SysTipEnum.QUARTZ_JOB_PAGE_QUERY_ERR.toString(), e.getMessage());
            return Result.errorsFormat(SysTipEnum.QUARTZ_JOB_PAGE_QUERY_ERR, e.getMessage());
        }
    }

    /**
     * 定时任务添加
     *
     * @param sysQuartzJobAddRo - 定时任务添加对象
     * @return null -
     * @author XinLau
     * @creed The only constant is change ! ! !
     * @since 2020/6/16 9:59
     */
    public Result addQuartzJob(final SysQuartzJobAddRO sysQuartzJobAddRo) {
//         1.校验定时任务重复
        Result result = quartzJobAddVerification(sysQuartzJobAddRo);
        if (!result.isSuccess()) {
            return result;
        }
//        2.创建新定时任务
        SysQuartzJob sysQuartzJob = SysQuartzJobConverter.INSTANCE.addRo2domain(sysQuartzJobAddRo);
        // 新创建定时任务，定时任务计划 内状态为不存在
        sysQuartzJob.setJobState(QuartzJobStatusEnum.NONE.getKey());
        if (serviceBySysQuartzJobService.saveQuartzJob(sysQuartzJob)) {
            addLog(CommonConstant.LOG_TYPE_OPERATE, CommonConstant.OPERATE_TYPE_ADD, SysTipEnum.QUARTZ_JOB_ADD_OK.toString(), sysQuartzJob.getJobName());
            return Result.ok();
        }
//        添加失败
        addLog(CommonConstant.LOG_TYPE_OPERATE, CommonConstant.OPERATE_TYPE_ADD, SysTipEnum.QUARTZ_JOB_ADD_ERR.toString(), sysQuartzJobAddRo.getJobName());
        return result.errorFormat(SysTipEnum.QUARTZ_JOB_ADD_ERR, sysQuartzJobAddRo.getJobName());
    }

    /**
     * 定时任务删除
     *
     * @param quartzJobIdentification - 定时任务标识
     * @return null -
     * @author XinLau
     * @creed The only constant is change ! ! !
     * @since 2020/6/16 9:59
     */
    public Result deleteQuartzJob(final Long quartzJobIdentification) {
//        1.删除定时任务校验
        SysQuartzJob sysQuartzJob = serviceBySysQuartzJobService.getById(quartzJobIdentification);
        Result result = quartzJobDeleteVerification(sysQuartzJob);
        if (!result.isSuccess()) {
            return result;
        }
//        2.删除定时任务
        boolean deleteRole = serviceBySysQuartzJobService.deleteQuartzJob(quartzJobIdentification);
        if (!deleteRole) {
//            删除定时任务失败
            addLog(CommonConstant.LOG_TYPE_OPERATE, CommonConstant.OPERATE_TYPE_DELETE, SysTipEnum.QUARTZ_JOB_DELETE_ERR.toString(), sysQuartzJob.getJobName());
            return result.errorFormat(SysTipEnum.QUARTZ_JOB_DELETE_ERR, sysQuartzJob.getJobName());
        }
        addLog(CommonConstant.LOG_TYPE_OPERATE, CommonConstant.OPERATE_TYPE_DELETE, SysTipEnum.QUARTZ_JOB_DELETE_OK.toString(), sysQuartzJob.getJobName());
        return Result.ok();
    }

    /**
     * 定时任务查看
     *
     * @param quartzJobIdentification - 定时任务标识
     * @return null -
     * @author XinLau
     * @creed The only constant is change ! ! !
     * @since 2020/6/16 9:59
     */
    public Result queryQuartzJobById(final Long quartzJobIdentification) {
        SysQuartzJob sysQuartzJob = serviceBySysQuartzJobService.getById(quartzJobIdentification);
        if (sysQuartzJob == null) {
            addLog(CommonConstant.LOG_TYPE_OPERATE, CommonConstant.OPERATE_TYPE_QUERY, SysTipEnum.QUARTZ_JOB_QUERY_ERR.toString());
            return Result.errorsFormat(SysTipEnum.QUARTZ_JOB_QUERY_ERR);
        }
        return Result.ok(SysQuartzJobConverter.INSTANCE.po2Vo(sysQuartzJob));
    }

    /**
     * 定时任务更新
     *
     * @param sysQuartzJobUpdateRo - 定时任务更新对象
     * @return null -
     * @author XinLau
     * @creed The only constant is change ! ! !
     * @since 2020/6/16 9:59
     */
    public Result updateQuartzJob(final SysQuartzJobUpdateRO sysQuartzJobUpdateRo) {
//        1.修改定时任务校验
        Result<SysQuartzJobVO> result = quartzJobUpdateVerification(sysQuartzJobUpdateRo);
        if (!result.isSuccess()) {
            return result;
        }
//        2.定时任务修改
        boolean update = serviceBySysQuartzJobService.updateByQuartzJobIdentification(sysQuartzJobUpdateRo);
        if (update) {
            return Result.ok();
        }
        addLog(CommonConstant.LOG_TYPE_OPERATE, CommonConstant.OPERATE_TYPE_ADD, SysTipEnum.QUARTZ_JOB_UPDATE_ERR.toString(), sysQuartzJobUpdateRo.getJobName());
        return result.errorFormat(SysTipEnum.QUARTZ_JOB_UPDATE_ERR, sysQuartzJobUpdateRo.getJobName());
    }

    /**
     * 开启构建定时任务
     *
     * @param quartzJob - 定时任务
     * @return Result -
     * @author XinLau
     * @creed The only constant is change ! ! !
     * @since 2020/6/15 10:57
     */
    public Result schedulerQuartzJob(final SysQuartzJob quartzJob) {
        try {
            // 1.定时任务执行类类名
            Class aClass = Class.forName(quartzJob.getJobClassName());
            // 2.定时任务构建器
            JobBuilder jobBuilder = JobBuilder.newJob(aClass)
                    .withIdentity(quartzJob.getJobName(), quartzJob.getJobGroup())
                    .withDescription(quartzJob.getDescription());
            if (ConvertUtils.isNotEmpty(quartzJob.getJobDataMap())) {
                // 3.构建定时任务参数
                JobDataMap jobDataMap = JSONObject.parseObject(quartzJob.getJobDataMap(), JobDataMap.class);
                jobBuilder.setJobData(jobDataMap);
            }
            // 4.检验定时任务执行类是否存在
            JobDetail jobDetail = jobBuilder.build();
            // 5.1触发时间点
            CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule(quartzJob.getCronExpression().trim());
            // 5.2触发器生成
            Trigger trigger = TriggerBuilder.newTrigger().withIdentity(quartzJob.getJobName(), quartzJob.getJobGroup())
                    .startNow().withSchedule(cronScheduleBuilder).build();
            // 6.交由Scheduler安排触发
            scheduler.scheduleJob(jobDetail, trigger);
            // 7.修改定时任务状态
            return updateQuartzJobState(quartzJob);
        } catch (ClassNotFoundException e) {
            addLog(CommonConstant.LOG_TYPE_OPERATE, CommonConstant.OPERATE_TYPE_QUERY, SysTipEnum.SC_ERROR_CLASS_NOT_FOUND_EXCEPTION.toString(), e.getMessage());
            return Result.errorsFormat(SysTipEnum.SC_ERROR_CLASS_NOT_FOUND_EXCEPTION, e.getMessage());
        } catch (SchedulerException e) {
            addLog(CommonConstant.LOG_TYPE_OPERATE, CommonConstant.OPERATE_TYPE_QUERY, SysTipEnum.SC_ERROR_SCHEDULER_EXCEPTION.toString(), e.getMessage());
            // 修改定时任务状态
            updateQuartzJobState(quartzJob);
            return Result.errorsFormat(SysTipEnum.SC_ERROR_SCHEDULER_EXCEPTION, e.getMessage());
        }
    }

    /**
     * 定时任务构建接口
     *
     * @param quartzJobIdentification - 定时任务标识
     * @return Result -
     * @author XinLau
     * @creed The only constant is change ! ! !
     * @since 2020/6/16 16:28
     */
    public Result schedulerQuartzJobByQuartzJobIdentification(final Long quartzJobIdentification) {
        SysQuartzJob sysQuartzJob = serviceBySysQuartzJobService.getById(quartzJobIdentification);
        Result result = schedulerQuartzJobVerification(sysQuartzJob);
        if (!result.isSuccess()) {
            return result;
        }
        return schedulerQuartzJob(sysQuartzJob);
    }

    /**
     * 定时任务执行接口
     *
     * @param quartzJobIdentification - 定时任务标识
     * @return Result -
     * @author XinLau
     * @creed The only constant is change ! ! !
     * @since 2020/6/16 16:28
     */
    public Result triggerQuartzJobByQuartzJobIdentification(final Long quartzJobIdentification) {
        // 1.定时任务验证
        SysQuartzJob sysQuartzJob = serviceBySysQuartzJobService.getById(quartzJobIdentification);
        Result result = triggerQuartzJobVerification(sysQuartzJob);
        if (!result.isSuccess()) {
            return result;
        }
        try {
            // 2.定时任务执行 执行一次，但是不更新状态
            JobKey key = new JobKey(sysQuartzJob.getJobName(), sysQuartzJob.getJobGroup());
            scheduler.triggerJob(key);
            return Result.ok();
        } catch (SchedulerException e) {
            addLog(CommonConstant.LOG_TYPE_OPERATE, CommonConstant.OPERATE_TYPE_QUERY, SysTipEnum.QUARTZ_JOB_TRIGGER_ERR.toString(), sysQuartzJob.getJobName(), e.getMessage());
            // 修改定时任务状态
            updateQuartzJobState(sysQuartzJob);
            return Result.errorsFormat(SysTipEnum.QUARTZ_JOB_TRIGGER_ERR, sysQuartzJob.getJobName(), e.getMessage());
        }
    }

    /**
     * 定时任务暂停接口
     *
     * @param quartzJobIdentification - 定时任务标识
     * @return Result -
     * @author XinLau
     * @creed The only constant is change ! ! !
     * @since 2020/6/16 16:28
     */
    public Result pauseQuartzJobByQuartzJobIdentification(final Long quartzJobIdentification) {
        SysQuartzJob sysQuartzJob = serviceBySysQuartzJobService.getById(quartzJobIdentification);
        Result result = pauseQuartzJobVerification(sysQuartzJob);
        if (!result.isSuccess()) {
            return result;
        }
        try {
            // 3.定时任务暂停
            JobKey key = new JobKey(sysQuartzJob.getJobName(), sysQuartzJob.getJobGroup());
            scheduler.pauseJob(key);
            // 4.修改定时任务状态
            return updateQuartzJobState(sysQuartzJob);
        } catch (SchedulerException e) {
            addLog(CommonConstant.LOG_TYPE_OPERATE, CommonConstant.OPERATE_TYPE_QUERY, SysTipEnum.QUARTZ_JOB_PAUSE_ERR.toString(), e.getMessage());
            // 修改定时任务状态
            updateQuartzJobState(sysQuartzJob);
            return Result.errorsFormat(SysTipEnum.QUARTZ_JOB_PAUSE_ERR, e.getMessage());
        }
    }

    /**
     * 定时任务恢复接口
     *
     * @param quartzJobIdentification - 定时任务标识
     * @return Result -
     * @author XinLau
     * @creed The only constant is change ! ! !
     * @since 2020/6/16 16:28
     */
    public Result resumeQuartzJobByQuartzJobIdentification(final Long quartzJobIdentification) {
        SysQuartzJob sysQuartzJob = serviceBySysQuartzJobService.getById(quartzJobIdentification);
        Result result = resumeQuartzJobVerification(sysQuartzJob);
        if (!result.isSuccess()) {
            return result;
        }
        try {
            // 2.定时任务恢复
            JobKey key = new JobKey(sysQuartzJob.getJobName(), sysQuartzJob.getJobGroup());
            scheduler.resumeJob(key);
            // 3.修改定时任务状态
            return updateQuartzJobState(sysQuartzJob);
        } catch (SchedulerException e) {
            addLog(CommonConstant.LOG_TYPE_OPERATE, CommonConstant.OPERATE_TYPE_QUERY, SysTipEnum.QUARTZ_JOB_PAUSE_ERR.toString(), e.getMessage());
            // 修改定时任务状态
            updateQuartzJobState(sysQuartzJob);
            return Result.errorsFormat(SysTipEnum.QUARTZ_JOB_PAUSE_ERR, e.getMessage());
        }
    }

    /**
     * 定时任务移除接口
     *
     * @param quartzJobIdentification - 定时任务标识
     * @return Result -
     * @author XinLau
     * @creed The only constant is change ! ! !
     * @since 2020/6/16 16:28
     */
    public Result removeQuartzJobByQuartzJobIdentification(final Long quartzJobIdentification) {
        SysQuartzJob sysQuartzJob = serviceBySysQuartzJobService.getById(quartzJobIdentification);
        Result result = removeQuartzJobVerification(sysQuartzJob);
        if (!result.isSuccess()) {
            return result;
        }
        try {
            JobKey key = new JobKey(sysQuartzJob.getJobName(), sysQuartzJob.getJobGroup());
            TriggerKey triggerKey = TriggerKey.triggerKey(sysQuartzJob.getJobName(), sysQuartzJob.getJobGroup());
            // 3.定时任务停止
            scheduler.pauseTrigger(triggerKey);
            // 4.移除触发器
            scheduler.unscheduleJob(triggerKey);
            // 5.删除任务
            scheduler.deleteJob(key);
            // 6.修改定时任务状态
            return updateQuartzJobState(sysQuartzJob);
        } catch (SchedulerException e) {
            addLog(CommonConstant.LOG_TYPE_OPERATE, CommonConstant.OPERATE_TYPE_QUERY, SysTipEnum.QUARTZ_JOB_REMOVE_ERR.toString(), e.getMessage());
            // 修改定时任务状态
            updateQuartzJobState(sysQuartzJob);
            return Result.errorsFormat(SysTipEnum.QUARTZ_JOB_REMOVE_ERR, e.getMessage());
        }
    }

    /**
     * 获取定时任务状态
     *
     * @param quartzJobIdentification - 定时任务标识
     * @return Result -
     * @author XinLau
     * @creed The only constant is change ! ! !
     * @since 2020/6/16 16:28
     */
    public Result getQuartzJobStateByQuartzJobIdentification(final Long quartzJobIdentification) {
        SysQuartzJob sysQuartzJob = serviceBySysQuartzJobService.getById(quartzJobIdentification);
        if (sysQuartzJob != null) {
            TriggerKey triggerKey = TriggerKey.triggerKey(sysQuartzJob.getJobName(), sysQuartzJob.getJobGroup());
            try {
                // TriggerState = NONE, NORMAL, PAUSED, COMPLETE, ERROR, BLOCKED
                Trigger.TriggerState triggerState = scheduler.getTriggerState(triggerKey);
                return Result.ok(triggerState.toString());
            } catch (SchedulerException e) {
                addLog(CommonConstant.LOG_TYPE_OPERATE, CommonConstant.OPERATE_TYPE_QUERY, SysTipEnum.QUARTZ_JOB_GET_STATE_ERR.toString(), e.getMessage());
                return Result.errorsFormat(SysTipEnum.QUARTZ_JOB_GET_STATE_ERR, e.getMessage());
            }
        }
        addLog(CommonConstant.LOG_TYPE_OPERATE, CommonConstant.OPERATE_TYPE_ADD, SysTipEnum.QUARTZ_JOB_GET_STAT_ERR_JOB_NON.toString());
        return Result.errorsFormat(SysTipEnum.QUARTZ_JOB_GET_STAT_ERR_JOB_NON);
    }

    /**
     * 定时任务启动初始化
     *
     * @return result -
     * @author XinLau
     * @creed The only constant is change ! ! !
     * @since 2020/7/23 10:46
     */
    public Result loadSysJobToQuartz() {
        Result result = new Result();
        List<SysQuartzJob> sysQuartzJobList = serviceBySysQuartzJobService.list();
        for (SysQuartzJob sysQuartzJob : sysQuartzJobList) {
            // 开启构建定时任务
            schedulerQuartzJob(sysQuartzJob);
            log.debug(SysTipEnum.QUARTZ_JOB_TRIGGER_OK.getValue(), sysQuartzJob.getJobName());
            if (QuartzJobStatusEnum.PAUSED.getKey().equals(sysQuartzJob.getJobState())) {
                // 暂停定时任务
                pauseQuartzJobByQuartzJobIdentification(sysQuartzJob.getId());
                log.debug(SysTipEnum.QUARTZ_JOB_PAUSE_OK.getValue(), sysQuartzJob.getJobName());
            }
        }
        return result;
    }

}